﻿@{
    Layout = "~/Views/Shared/_JQBaseLayOut.cshtml";
    var IsAdminUser = (bool)ViewBag.IsAdminUser;
}
@using Model

@section JQscriptSection{
    <script type="text/javascript">

    $(document).ready(function ()
    {
        InitGrid();

        initInputValidate();

        HandleDom.ClassTriggerClick("icon-add", function ()
        {
            $('#hiddenLOCATIONID').val();
            HandleDialog.OpenDialogByAdd('dlg', '添加货位信息', function ()
            {
                //HandleData.ClearForm('formEdit');
                $("#formEdit")[0].reset();
               // $("#USERID").val(-1);
                changeUserID(0);

            });
        });

        HandleDom.ClassTriggerClick("icon-edit", function ()
        {
            var row = HandleData.GetDataGridSelected("dg");
            edit(row);
        });

        HandleDom.ClassTriggerClick("icon-remove", function ()
        {
            var row = HandleData.GetDataGridSelected("dg");
            deleteData(row);
        });

        HandleDom.ClassTriggerClick("icon-reload", function ()
        {
            InitGrid();
        });
    });

    function initInputValidate()
    {
        $('#USERCODE').validatebox({
            required: true,
            invalidMessage: "用户账号已存在",
            // validType: { "remote": "['@Url.Action("ExistsUserCode", "PermissionManage")', 'USERCODE', 'USERID']" },

        });

        $('#USERNAME').validatebox({
            required: true,
            // validType: 'email'
        });

        $('#PASSWORD').validatebox({
            required: true,
            validType: 'minLength[3]'
        });
    }

    function changeUserID(userId)
    {
        var $userId = $("#USERID");
        var $userCode = $("#USERCODE");
        $userId.val(userId);
        var validtype = $userCode.attr("validtype");   //remote['/PermissionManage/ExistsUserCode?USERID=-1', 'USERCODE']
        var value = validtype.substring(validtype.indexOf("=")+1, validtype.indexOf("',"));        
        if (value != userId)
        {
            validtype = validtype.replace(value, userId);
            $userCode.attr("validtype", validtype);
            $userCode.validatebox({ validType: validtype });
        }
    }

    //编辑
    function edit(row)
    {
        if (!row)
        {
            HandleTips.Alert("请选中数据");
            return;
        }

        HandleDialog.OpenDialog(row, 'dlg', '编辑用户信息', function ()
        {
            //$('#USERID').val(row.USERID);
            changeUserID(row.USERID);
            $('#USERCODE').val(row.USERCODE);
            $('#USERNAME').val(row.USERNAME);
            $('#PASSWORD').val(row.PASSWORD);
        });
    }

    function saveData()
    {


        HandleRequest.submitForm('formEdit', '@Url.Action("SaveUser", "PermissionManage")', function (response)
        {
            response = $.parseJSON(response);
            $.log('saveData.response=%s', response);
            if (response.IsSuccess)
            {
                // layer.alert(response.Message, 9);
                InitGrid();
                HandleDialog.CloseDialog('dlg');
            }
            else
            {
                //layer.msg(response.Message, 1);
            }
            HandleTips.Alert(response.Message);
        }, function ()            
        {
            var isValid = $("#formEdit").form('validate');          
            return isValid;	// 返回false将停止form提交
        }
        );
    }

    function deleteData(row)
    {
        if (!row)
        {
            HandleTips.Alert("请选中数据");
            return;
        }

        HandleTips.confirm("确定要删除吗", function (r)
        {
            if (!r) return;

            $.ajaxPost('@Url.Action("DeleteUser", "PermissionManage")', row, function (response)
            {
                if (response.IsSuccess)
                {
                    InitGrid();
                }
                HandleTips.Alert(response.Message);
            });
        });
    }

    function InitGrid()
    {
        var queryParams = $.formToJson("query");
        $.log(queryParams);
        var otherParamm = { toolbar: "toolbar", idField: "USERID" };
        //表格的列设置
        var columns = [[
               { field: 'USERCODE', title: '用户账号', width: 150, },
               { field: 'USERNAME', title: '用户名', width: 150, },


        ]];
        HandleData.InitDataGridPage('dg', '/PermissionManage/GetUserList', queryParams,
            columns,
            otherParamm,
            function (data)
            { },
            function (rowIndex, rowData)//双击
            {
                edit(rowData);
            },
            null
        );
    }
</script>
}

<body style="background-color: ButtonFace; font-size: 17px">
    <div class="easyui-panel" title="查询条件" style="height: 70px; padding: 5px; background: #fafafa; width: 1500px;">
        <form id="query" method="post">
            <table>           
                <tr>
                    <td>
                        用户账号:
                    </td>
                    <td>
                        <input id="txtUSERCODE" name="USERCODE" type="search" style="width: 154px" />
                    </td>
                    <td>
                        用户名:
                    </td>
                    <td>
                        <input id="txtUSERNAME" name="USERNAME" type="search" style="width: 154px" />
                    </td>
                    <td>
                        <a id="btnSerach" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="InitGrid()">查询</a>
                    </td>
                </tr>
            </table>
        </form>
        @*<form id="import" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>
                        <input id="FileUpload" type="file" name="files" style="width: 250px; height: 24px; background: White"
                               class="easyui-validatebox" />
                        <a id="btnGet" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-excel'" onclick="ImpotExcel()">导入</a>
                    </td>
                </tr>
            </table>
        </form>*@
    </div>
    <div style="min-height: 720px; width: 1500px;">
        <table id="dg" title="用户列表" style="min-height: 600px;">          
        </table>
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 550px; height: 200px; top: 300px; left: 400px; padding: 2px"
         data-options="closed:true,buttons:'#dlg-buttons',modal:true">
        <form id="formEdit" method="post">
            <input id="USERID" name="USERID" type="hidden" />
            <table>
                <tr>
                    <td>
                        用户账号:
                    </td>
                    <td>                        
                        <input id="USERCODE" name="USERCODE" maxlength="128" style="width: 154px"
                               validtype="remote['@Url.Action("ExistsUserCode", "PermissionManage", new { USERID=-1})','USERCODE']" />                               
                        @*validtype="remote['@Url.Action("ExistsUserCode", "PermissionManage")']"*@
                    </td>
                    <td>
                        用户名:
                    </td>
                    <td >
                        <input id="USERNAME" name="USERNAME" type="search" style="width: 154px" />
                    </td>
                </tr>
                <tr>
                    <td>
                        密码:
                    </td>
                    <td>
                        <input id="PASSWORD" name="PASSWORD" type="password" maxlength="128" style="width: 154px" />
                    </td>
                    <td></td>
                    <td></td>
                </tr>               
                <tr>
                    <td></td>
                    <td>
                        <a id="btnSave" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveData()">保存</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    @Html.Partial("~/Views/Shared/_PartialToolbar.cshtml", new Toolbar
{
    GridName = "dg",
    GridType = "datagrid",
    ToolbarName = "toolbar",
    FunctionModule = new Dictionary<string, List<FunctionModule>>()
    {
        {"Function",new List<FunctionModule>()
            {
              new FunctionModule(){Name = "Add",IsEnable =IsAdminUser },
              new FunctionModule(){Name = "Edit",IsEnable =true },
              new FunctionModule(){Name = "Remove",IsEnable =IsAdminUser },
              new FunctionModule(){Name = "Reload",IsEnable =true },
              new FunctionModule(){Name = "Collapse",IsEnable =false },
              new FunctionModule(){Name = "Exporter",IsEnable =false },
              new FunctionModule(){Name = "AllAdd",IsEnable =true },
            }
        }
    }
})

</body>
